﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.Common.Entity.ParamsVerify.VerifyAttribute
 * 接口名称 IDCardVerifyAttribute
 * 开发人员：11920
 * 创建时间：2023/6/16 11:53:10
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools.Verify
{
	/// <summary>
	/// 身份证验证
	/// </summary>
	public class IDCardVerifyAttribute : BaseVerifyAttribute
	{
		/// <summary>
		/// 属性名称
		/// </summary>
		/// <param name="name"></param>
		public IDCardVerifyAttribute(string name) : base(name)
		{
			this._errorInfo = "身份证填写错误!";
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override string GetErrorInfo()
		{
			return base.GetErrorInfo();
		}
		/// <summary>
		/// 身份证验证
		/// item1 msg 
		/// item2 is false 表示验证不合法
		/// </summary>
		/// <param name="o"></param>
		/// <param name="prop"></param>
		/// <returns></returns>
		public override Tuple<string, bool> Verify(object o, PropertyInfo prop)
		{
			string regex_Version_1 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$";
			string regex_Version_2= "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";


			Regex regex_version_1 = new Regex(regex_Version_1);
			Regex regex_version_2 = new Regex(regex_Version_2);
			object value = prop.GetValue(o);
			bool IsOk = false;
			if (value!=null&& value.ToString()!=string.Empty)
			{
				var match_v1 = regex_version_1.Match(value.ToString());
				if (match_v1.Success)
				{
					IsOk = match_v1.Success;
					return Tuple.Create(GetErrorInfo(), IsOk);
				}
				var match_v2 = regex_version_2.Match(value.ToString());
				if (match_v2.Success)
				{
					IsOk = match_v2.Success;
					return Tuple.Create(GetErrorInfo(), IsOk);
				}
			}
			return Tuple.Create(GetErrorInfo(),IsOk);
		}

	}
}
